package mpc;

import gui.Визуализатор_для_Скобок;

import java.awt.Color;
import java.util.HashMap;

import javax.swing.JOptionPane;

public class Транслятор_Строки {
	private static HashMap<String, Integer> operators;
	private static Tree_Node buildUnlistedTree(String equation, int beginIndex, int endIndex){
		/*
		if(endIndex - beginIndex < 1)
			return new Tree_Node("0", Color.red);
		*/
		
		int currPos = endIndex, bestStackLevel = 1000, currStackLevel = 0, currPriority, bestPriority = 1000, 
				bestPosition = -1;
		
		while(currPos > beginIndex){
			currPos--;
			if(equation.charAt(currPos) == ')'){
				currStackLevel++;
			} else if(equation.charAt(currPos) == '('){
				currStackLevel--;
			} else if(operators.containsKey(equation.substring(currPos, currPos + 1))){
				currPriority = operators.get(equation.substring(currPos, currPos + 1));
				if((currStackLevel == bestStackLevel) && (currPriority < bestPriority)){
					//bestStackLevel = currStackLevel;
					bestPriority = currPriority;
					bestPosition = currPos;
				} else if(currStackLevel < bestStackLevel){
					bestStackLevel = currStackLevel;
					bestPriority = currPriority;
					bestPosition = currPos;
				}
			}
		}
		
		Tree_Node r;
		if(bestPosition == -1){
			while((beginIndex < endIndex) && !isGoodCharacter(equation.charAt(beginIndex)))
				beginIndex++;
			
			while((beginIndex < endIndex) && !isGoodCharacter(equation.charAt(endIndex - 1)))
				endIndex--;
			
			
			r = (beginIndex < endIndex) ? 
					new Tree_Node(equation.substring(beginIndex, endIndex), Color.getHSBColor(0.35f, 1.0f, 0.7f))
			: new Tree_Node("0", Color.red);
			//r = buildUnlistedTree(equation, beginIndex, endIndex);
		} else {
			String b = equation.substring(bestPosition, bestPosition + 1);
			
			if(b.equals("*"))
				b = "×";
			else if(b.equals("/"))
				b = "÷";
			
			r = new Tree_Node(b, Color.black);
			r.addLeftSone(buildUnlistedTree(equation, beginIndex, bestPosition));
			r.addRightSone(buildUnlistedTree(equation, bestPosition + 1, endIndex));
		}
		
		return r;
	}
	
	private static boolean isGoodCharacter(char c){
		return ((('a' <= c) && (c <= 'z')) || (('A' <= c) && (c <= 'Z')) || (('0' <= c) && (c <= '9')));
	}
	
	public static Tree_Node getTree(String equation){
		if(!isCorrectCues(equation)){
			JOptionPane.showMessageDialog(Визуализатор_для_Скобок.WorkFrame, "В скобочной последовательности ошибка.", "Ошибка!", 
					JOptionPane.ERROR_MESSAGE);
			return null;
		} 
			{
				operators = new HashMap<String, Integer>();
				operators.put("+", 1);
				operators.put("-", 1);
				operators.put("*", 2);
				operators.put("/", 2);
				//operators.put("**", 3);
			}
			Tree_Node root = buildUnlistedTree(equation, 0, equation.length());
			//root.initSituation();
			return root;
	}
	
	public static boolean isCorrectCues(String equation){
		int stack = 0;
		
		for(int i = 0; i < equation.length(); i++){
			if(equation.charAt(i) == '(')
				stack++;
			if(equation.charAt(i) == ')')
				stack--;
			
			if (stack < 0)
				return false;
		}
		
		return stack == 0;
	}
}
